﻿using System;
using System.Linq;
using System.Xml.Linq;

namespace JinJiang.BookingEngine.Services
{
    public class MemberInfo
    {
        private readonly XDocument _document;

        public MemberInfo(string responseXml)
        {
            _document = XDocument.Parse(responseXml);
        }

        /// <summary>
        /// </summary>
        public bool IsSuccess
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var resultAttribute = _document.Root.Attribute("code");
                    if (resultAttribute != null)
                    {
                        return resultAttribute.Value.Equals("0", StringComparison.InvariantCultureIgnoreCase);
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("message").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        public string UserId
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("userId").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        public string FullName
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("fullName").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        public string FirstName
        {
            get
            {
                var fullName = FullName;
                var nameArray = fullName.Split(' ');
                if (nameArray.Length > 1)
                {
                    return nameArray[0];
                }
                if (fullName.Length > 1)
                {
                    return fullName.Substring(1, fullName.Length - 1);
                }
                return string.Empty;
            }
        }

        public string LastName
        {
            get
            {
                var fullName = FullName;
                var nameArray = fullName.Split(' ');
                if (nameArray.Length > 1)
                {
                    return nameArray[1];
                }
                if (fullName.Length > 1)
                {
                    return fullName.Substring(0, 1);
                }
                return string.Empty;
            }
        }

        public string CardLevel
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("cardLevel").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        public string Point
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("point").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        public string Mobile
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("mobile").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        public string Email
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("email").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        public string Address
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("address").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        public string Flag
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("flag").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }
    }
}